home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / fab < prev    next >
Encoding:
Text File  |  2007-04-04  |  24.8 KB  |  849 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '2.2'
  24. __title__ = "Fax Address Book"
  25. __doc__ = "A simple fax address book for HPLIP."
  26.  
  27. import cmd
  28. from base.g import *
  29. from base import utils
  30. import getopt
  31.  
  32. log.set_module("hp-fab")
  33.  
  34.  
  35. def additional_copyright():
  36.     log.info("Includes code from KirbyBase 1.8.1")
  37.     log.info("Copyright (c) Jamey Cribbs (jcribbs@twmi.rr.com)")
  38.     log.info("Licensed under the Python Software Foundation License.")
  39.     log.info("")
  40.  
  41. USAGE = [(__doc__, "", "name", True),
  42.          ("Usage: hp-fab [MODE] [OPTIONS]", "", "summary", True),
  43.          ("[MODE]", "", "header", False),
  44.          ("Enter interactive mode:", "-i or --interactive (see Note 1)", "option", False),
  45.          ("Enter graphical UI mode:", "-u or --gui (Default)", "option", False),
  46.          utils.USAGE_SPACE,
  47.          utils.USAGE_OPTIONS,
  48.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  49.          utils.USAGE_HELP,
  50.          utils.USAGE_NOTES,
  51.          ("1. Use 'help' command at the fab > prompt for command help (interactive mode (-i) only).", "", "note", False),
  52.          utils.USAGE_SPACE,
  53.          utils.USAGE_SEEALSO,
  54.          ("hp-sendfax", "", "seealso", False),
  55.          ]
  56.  
  57. def usage(typ='text'):
  58.     if typ == 'text':
  59.         utils.log_title(__title__, __version__)
  60.         additional_copyright()
  61.  
  62.     utils.format_text(USAGE, typ, __title__, 'hp-fab', __version__)
  63.     sys.exit(0)
  64.  
  65.  
  66. ## Console class (from ASPN Python Cookbook)
  67. ## Author:   James Thiele
  68. ## Date:     27 April 2004
  69. ## Version:  1.0
  70. ## Location: http://www.eskimo.com/~jet/python/examples/cmd/
  71. ## Copyright (c) 2004, James Thiele
  72. class Console(cmd.Cmd):
  73.  
  74.     def __init__(self):
  75.         cmd.Cmd.__init__(self)
  76.         self.intro  = "Type 'help' for a list of commands. Type 'exit' or 'quit' to quit."
  77.         self.db =  fax.FaxAddressBook() # kirbybase instance
  78.         self.prompt = utils.bold("hp-fab > ")
  79.  
  80.     ## Command definitions ##
  81.     def do_hist(self, args):
  82.         """Print a list of commands that have been entered"""
  83.         print self._hist
  84.  
  85.     def do_exit(self, args):
  86.         """Exits from the console"""
  87.         return -1
  88.  
  89.     def do_quit(self, args):
  90.         """Exits from the console"""
  91.         return -1
  92.  
  93.     ## Command definitions to support Cmd object functionality ##
  94.     def do_EOF(self, args):
  95.         """Exit on system end of file character"""
  96.         return self.do_exit(args)
  97.  
  98.     def do_help(self, args):
  99.         """Get help on commands
  100.            'help' or '?' with no arguments prints a list of commands for which help is available
  101.            'help <command>' or '? <command>' gives help on <command>
  102.         """
  103.         ## The only reason to define this method is for the help text in the doc string
  104.         cmd.Cmd.do_help(self, args)
  105.  
  106.     ## Override methods in Cmd object ##
  107.     def preloop(self):
  108.         """Initialization before prompting user for commands.
  109.            Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
  110.         """
  111.         cmd.Cmd.preloop(self)   ## sets up command completion
  112.         self._hist    = []      ## No history yet
  113.         self._locals  = {}      ## Initialize execution namespace for user
  114.         self._globals = {}
  115.  
  116.         self.do_list('')
  117.  
  118.     def postloop(self):
  119.         """Take care of any unfinished business.
  120.            Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
  121.         """
  122.         cmd.Cmd.postloop(self)   ## Clean up command completion
  123.         print "Exiting..."
  124.  
  125.     def precmd(self, line):
  126.         """ This method is called after the line has been input but before
  127.             it has been interpreted. If you want to modifdy the input line
  128.             before execution (for example, variable substitution) do it here.
  129.         """
  130.         self._hist += [line.strip()]
  131.         return line
  132.  
  133.     def postcmd(self, stop, line):
  134.         """If you want to stop the console, return something that evaluates to true.
  135.            If you want to do some post command processing, do it here.
  136.         """
  137.         return stop
  138.  
  139.     def emptyline(self):
  140.         """Do nothing on empty input line"""
  141.         pass
  142.  
  143.     def default(self, line):
  144.         print utils.red("error: Unrecognized command. Use 'help' to list commands.")
  145.  
  146.     def get_nickname(self, args, fail_if_match=True, alt_text=False):
  147.         if not args:
  148.             while True:
  149.                 if alt_text:
  150.                     nickname = raw_input(utils.bold("Enter the entry name (nickname) to add (<enter>=done*, c=cancel) ? ")).strip()
  151.                 else:
  152.                     nickname = raw_input(utils.bold("Enter the entry name (nickname) (c=cancel) ? ")).strip()
  153.  
  154.                 if nickname.lower() == 'c':
  155.                     print utils.red("Canceled")
  156.                     return ''
  157.  
  158.                 if not nickname:
  159.                     if alt_text:
  160.                         return ''
  161.                     else:
  162.                         print utils.red("error: Nickname must not be blank.")
  163.                         continue
  164.  
  165.  
  166.                 if fail_if_match:
  167.                     if self.db.select(['name'], [nickname]):
  168.                         print utils.red("error: Entry already exists. Please choose a different name.")
  169.                         continue
  170.  
  171.                 else:
  172.                     if not self.db.select(['name'], [nickname]):
  173.                         print utils.red("error: Entry not found. Please enter a different name.")
  174.                         continue
  175.  
  176.                 break
  177.  
  178.         else:
  179.             nickname = args.strip()
  180.  
  181.             if fail_if_match:
  182.                 if self.db.select(['name'], [nickname]):
  183.                     print utils.red("error: Entry already exists. Please choose a different name.")
  184.                     return ''
  185.  
  186.             else:
  187.                 if not self.db.select(['name'], [nickname]):
  188.                     print utils.red("error: Entry not found. Please enter a different name.")
  189.                     return ''
  190.  
  191.         return nickname
  192.  
  193.  
  194.     def get_groupname(self, args, fail_if_match=True, alt_text=False):
  195.         all_groups = self.db.AllGroups()
  196.  
  197.         if not args:
  198.             while True:
  199.                 if alt_text:
  200.                     groupname = raw_input(utils.bold("Enter the group name to join (<enter>=done*, c=cancel) ? ")).strip()
  201.                 else:
  202.                     groupname = raw_input(utils.bold("Enter the group name (c=cancel) ? ")).strip()
  203.  
  204.  
  205.                 if groupname.lower() == 'c':
  206.                     print utils.red("Canceled")
  207.                     return ''
  208.  
  209.                 if not groupname:
  210.                     if alt_text:
  211.                         return ''
  212.                     else:
  213.                         print utils.red("error: The group name must not be blank.")
  214.                         continue
  215.  
  216.                 if fail_if_match: 
  217.                     if groupname in all_groups:
  218.                         print utils.red("error: Entry already exists. Please choose a different name.")
  219.                         continue
  220.  
  221.                 else:
  222.                     if groupname not in all_groups:
  223.                         print utils.red("error: Entry not found. Please enter a different name.")
  224.                         continue
  225.  
  226.                 break
  227.  
  228.         else:
  229.             groupname = args.strip()
  230.  
  231.             if fail_if_match: 
  232.                 if groupname in all_groups:
  233.                     print utils.red("error: Entry already exists. Please choose a different name.")
  234.                     return ''
  235.  
  236.             else:
  237.                 if groupname not in all_groups:
  238.                     print utils.red("error: Entry not found. Please enter a different name.")
  239.                     return ''
  240.  
  241.         return groupname
  242.  
  243.     def do_list(self, args):
  244.         """ 
  245.         List entries and/or groups.
  246.         list [groups|entries|all|]
  247.         dir [groups|entries|all|]
  248.         """
  249.  
  250.         if args:
  251.             scope = args.strip().split()[0]
  252.  
  253.             if args.startswith('ent'):
  254.                 self.do_entries('')
  255.                 return
  256.             elif args.startswith('gro'):
  257.                 self.do_groups('')
  258.                 return
  259.  
  260.         self.do_entries('')
  261.         self.do_groups('')
  262.  
  263.     do_dir = do_list
  264.  
  265.     def do_entries(self, args):
  266.         """
  267.         List entries.
  268.         entries
  269.         """
  270.         all_entries = self.db.AllRecordEntries()
  271.         log.debug(all_entries)
  272.  
  273.         print utils.bold("\nEntries:\n")
  274.         if len(all_entries) > 0:
  275.  
  276.             formatter = utils.TextFormatter(
  277.                             (
  278.                                 {'width': 28, 'margin' : 2},
  279.                                 {'width': 28, 'margin' : 2},
  280.                                 {'width': 58, 'margin' : 2},
  281.                             )
  282.                         )
  283.  
  284.             print formatter.compose(("Name", "Fax Number", "Member of Group(s)"))
  285.             print formatter.compose(('-'*28, '-'*28, '-'*58))
  286.  
  287.             # TODO: Sort the list by (nick)name
  288.             for abe in all_entries:
  289.                 print formatter.compose((abe.name, abe.fax, abe.groups))
  290.         else:
  291.             print "(None)"
  292.  
  293.         print
  294.  
  295.     def do_groups(self, args):
  296.         """ 
  297.         List groups.
  298.         groups
  299.         """
  300.         all_groups = self.db.AllGroups()
  301.         log.debug(all_groups)
  302.  
  303.         print utils.bold("\nGroups:\n")
  304.         if len(all_groups):
  305.  
  306.             formatter = utils.TextFormatter(
  307.                             (
  308.                                 {'width': 28, 'margin' : 2},
  309.                                 {'width': 58, 'margin' : 2},
  310.                             )
  311.                         )
  312.             print formatter.compose(("Group", "Members"))
  313.             print formatter.compose(('-'*28, '-'*58))
  314.  
  315.             # TODO: Sort the list by group name
  316.             for group in all_groups:
  317.                 print formatter.compose((group, ', '.join(self.db.GroupEntries(group))))
  318.         else:
  319.             print "(None)"
  320.  
  321.         print
  322.  
  323.  
  324.     def do_edit(self, args):
  325.         """
  326.         Edit an entry.
  327.         edit [entry]
  328.         modify [entry]
  329.         """
  330.         nickname = self.get_nickname(args, fail_if_match=False)
  331.         if not nickname: return
  332.  
  333.         abe = fax.AddressBookEntry(self.db.select(['name'], [nickname])[0])
  334.         log.debug(abe)
  335.  
  336.         print utils.bold("\nEdit/modify entry information for %s:\n" % abe.name)
  337.  
  338.         save_title = abe.title
  339.         title = raw_input(utils.bold("Title (<enter>='%s', c=cancel)? " % save_title)).strip()
  340.  
  341.         if title.lower() == 'c':
  342.             print utils.red("Canceled")
  343.             return
  344.  
  345.         if not title:
  346.             title = save_title
  347.  
  348.         save_firstname = abe.firstname
  349.         firstname = raw_input(utils.bold("First name (<enter>='%s', c=cancel)? " % save_firstname)).strip()
  350.  
  351.         if firstname.lower() == 'c':
  352.             print utils.red("Canceled")
  353.             return
  354.  
  355.         if not firstname:
  356.             firstname = save_firstname
  357.  
  358.         save_lastname = abe.lastname
  359.         lastname = raw_input(utils.bold("Last name (<enter>='%s', c=cancel)? " % save_lastname)).strip()
  360.  
  361.         if lastname.lower() == 'c':
  362.             print utils.red("Canceled")
  363.             return
  364.  
  365.         if not lastname:
  366.             lastname = save_lastname
  367.  
  368.         save_faxnum = abe.fax
  369.         while True:
  370.             faxnum = raw_input(utils.bold("Fax Number (<enter>='%s', c=cancel)? " % save_faxnum)).strip()
  371.  
  372.             if faxnum.lower() == 'c':
  373.                 print utils.red("Canceled")
  374.                 return
  375.  
  376.             if not faxnum and not save_faxnum:
  377.                 print utils.red("error: Fax number must not be empty.")
  378.                 continue
  379.  
  380.             if not faxnum:
  381.                 faxnum = save_faxnum
  382.  
  383.             ok = True
  384.             for c in faxnum:
  385.                 if c not in '0123456789-(+) *#':
  386.                     print utils.red("error: Invalid characters in fax number. Fax number may only contain '0123456789-(+) '")
  387.                     ok = False
  388.                     break
  389.  
  390.  
  391.             if ok: break
  392.  
  393.         save_notes = abe.notes
  394.         notes = raw_input(utils.bold("Notes (<enter>='%s', c=cancel)? " % save_notes)).strip()
  395.  
  396.         if notes.lower() == 'c':
  397.             print utils.red("Canceled")
  398.             return
  399.  
  400.         if not notes:
  401.             notes = save_notes
  402.  
  403.         if abe.group_list:
  404.             print "\nLeave or Stay in a Group:\n"
  405.  
  406.         new_groups = []
  407.         for g in abe.group_list:
  408.             user_input = raw_input(utils.bold("Stay in group '%s' (y=yes*, n=no (leave), c=cancel) ? " % g)).strip().lower()
  409.  
  410.             if not user_input or user_input == 'y':
  411.                 new_groups.append(g)
  412.  
  413.             if user_input == 'c':
  414.                 print utils.red("Canceled")
  415.                 return
  416.  
  417.  
  418.         print "\nJoin New Group(s):\n"
  419.  
  420.         while True:
  421.             add_group = self.get_groupname('', fail_if_match=False, alt_text=True) 
  422.  
  423.             if add_group.lower() == 'c':
  424.                 print utils.red("Canceled")
  425.                 return
  426.  
  427.             if not add_group.lower():
  428.                 break
  429.  
  430.             all_groups = self.db.AllGroups()
  431.  
  432.             if add_group not in all_groups:
  433.                 log.warn("Group not found.")
  434.                 user_input = raw_input(utils.bold("Is this a new group (y=yes*, n=no) ?")).strip().lower()
  435.  
  436.                 if user_input == 'n':
  437.                     continue
  438.  
  439.             if add_group in abe.groups:
  440.                 log.error("error: Group already specified. Choose a different group name or press <enter> to continue.")
  441.                 continue
  442.  
  443.             new_groups.append(add_group)
  444.  
  445.  
  446.         self.db.update(['name'], [nickname], fax.AddressBookEntry((-1, nickname, title, firstname, lastname, faxnum, ','.join(new_groups), notes)))
  447.         self.do_show(nickname)
  448.  
  449.         print
  450.  
  451.     do_modify = do_edit
  452.  
  453.  
  454.     def do_editgrp(self, args):
  455.         """
  456.         Edit a group.
  457.         editgrp [group]
  458.         modifygrp [group]
  459.         """
  460.         group = self.get_groupname(args, fail_if_match=False)
  461.         if not group: return
  462.  
  463.         old_entries = self.db.GroupEntries(group)
  464.  
  465.         new_entries = []
  466.  
  467.         print "\nLeave or Remove Existing Entries in Group:\n"
  468.  
  469.         for e in old_entries:
  470.             user_input = raw_input(utils.bold("Leave entry '%s' in this group (y=yes*, n=no (remove), c=cancel) ? " % e)).lower().strip()
  471.  
  472.             if not user_input or user_input == 'y':
  473.                 new_entries.append(e)
  474.  
  475.             if user_input == 'c':
  476.                 print utils.red("Canceled")
  477.                 return
  478.  
  479.         print "\nAdd New Entries in Group:\n"
  480.  
  481.         while True:
  482.             nickname = self.get_nickname('', fail_if_match=False, alt_text=True)
  483.  
  484.             if nickname.lower() == 'c':
  485.                 print utils.red("Canceled")
  486.                 return
  487.  
  488.             if not nickname.lower():
  489.                 break
  490.  
  491.             new_entries.append(nickname)
  492.  
  493.         self.db.UpdateGroupEntries(group, new_entries)
  494.  
  495.         print
  496.  
  497.     do_modifygrp = do_editgrp
  498.  
  499.  
  500.     def do_add(self, args):
  501.         """
  502.         Add an entry.
  503.         add [entry]
  504.         new [entry]
  505.         """
  506.         nickname = self.get_nickname(args, fail_if_match=True)
  507.         if not nickname: return
  508.  
  509.         print utils.bold("\nEnter entry information for %s:\n" % nickname)
  510.  
  511.         title = raw_input(utils.bold("Title (c=cancel)? ")).strip()
  512.  
  513.         if title.lower() == 'c':
  514.             print utils.red("Canceled")
  515.             return
  516.  
  517.         firstname = raw_input(utils.bold("First name (c=cancel)? ")).strip()
  518.  
  519.         if firstname.lower() == 'c':
  520.             print utils.red("Canceled")
  521.             return
  522.  
  523.         lastname = raw_input(utils.bold("Last name (c=cancel)? ")).strip()
  524.  
  525.         if lastname.lower() == 'c':
  526.             print utils.red("Canceled")
  527.             return
  528.  
  529.         while True:
  530.             faxnum = raw_input(utils.bold("Fax Number (c=cancel)? ")).strip()
  531.  
  532.             if faxnum.lower() == 'c':
  533.                 print utils.red("Canceled")
  534.                 return
  535.  
  536.             if not faxnum:
  537.                 print utils.red("error: Fax number must not be empty.")
  538.                 continue
  539.  
  540.             ok = True
  541.             for c in faxnum:
  542.                 if c not in '0123456789-(+) *#':
  543.                     print utils.red("error: Invalid characters in fax number. Fax number may only contain '0123456789-(+) *#'")
  544.                     ok = False
  545.                     break
  546.  
  547.  
  548.             if ok: break
  549.  
  550.         notes = raw_input(utils.bold("Notes (c=cancel)? ")).strip()
  551.  
  552.         if notes.strip().lower() == 'c':
  553.             print utils.red("Canceled")
  554.             return
  555.  
  556.         groups = []
  557.         all_groups = self.db.AllGroups()
  558.         while True:
  559.             add_group = raw_input(utils.bold("Member of group (<enter>=done*, c=cancel) ?" )).strip()
  560.  
  561.             if add_group.lower() == 'c':
  562.                 print utils.red("Canceled")
  563.                 return
  564.  
  565.             if not add_group:
  566.                 break
  567.  
  568.             if add_group not in all_groups:
  569.                 log.warn("Group not found.")
  570.  
  571.                 while True:
  572.                     user_input = raw_input(utils.bold("Is this a new group (y=yes*, n=no) ?")).lower().strip()
  573.  
  574.                     if user_input not in ['', 'n', 'y']:
  575.                         log.error("Please enter 'y', 'n' or press <enter> for 'yes'.")
  576.                         continue
  577.  
  578.                     break
  579.  
  580.                 if user_input == 'n':
  581.                     continue
  582.  
  583.             if add_group in groups:
  584.                 log.error("Group already specified. Choose a different group name or press <enter> to continue.")
  585.                 continue
  586.  
  587.             groups.append(add_group)
  588.  
  589.         self.db.insert(fax.AddressBookEntry((-1, nickname, title, firstname, lastname, faxnum, ','.join(groups), notes)))
  590.         self.do_show(nickname)
  591.  
  592.  
  593.     do_new = do_add
  594.  
  595.  
  596.     def do_addgrp(self, args):
  597.         """
  598.         Add a group.
  599.         addgrp [group]
  600.         newgrp [group]
  601.         """
  602.         group = self.get_groupname(args, fail_if_match=True)
  603.         if not group: return
  604.  
  605.         entries = []
  606.         while True:
  607.             nickname = self.get_nickname('', fail_if_match=False, alt_text=True)
  608.  
  609.             if nickname.lower() == 'c':
  610.                 print utils.red("Canceled")
  611.                 return
  612.  
  613.             if not nickname.lower():
  614.                 break
  615.  
  616.             entries.append(nickname)
  617.  
  618.         self.db.UpdateGroupEntries(group, entries)
  619.  
  620.         print
  621.  
  622.     do_newgrp = do_addgrp
  623.  
  624.  
  625.     def do_view(self, args):
  626.         """
  627.         View all entry data.
  628.         view
  629.         """
  630.         all_entries = self.db.AllRecordEntries()
  631.         log.debug(all_entries)
  632.  
  633.         print utils.bold("\nView all Data:\n")
  634.         if len(all_entries) > 0:
  635.  
  636.             formatter = utils.TextFormatter(
  637.                             (
  638.                                 {'width': 20, 'margin' : 2}, # name
  639.                                 {'width': 20, 'margin' : 2}, # title
  640.                                 {'width': 20, 'margin' : 2}, # first
  641.                                 {'width': 20, 'margin' : 2}, # last
  642.                                 {'width': 20, 'margin' : 2}, # fax
  643.                                 {'width': 20, 'margin' : 2}, # notes
  644.                                 {'width': 20, 'margin' : 2}, # groups
  645.                                 {'width': 8, 'margin' : 2}, # recno
  646.                             )
  647.                         )
  648.  
  649.             print formatter.compose(("Name", "Title", "First Name", "Last Name", "Fax", "Notes", "Member of Group(s)", "(recno)"))
  650.             print formatter.compose(('-'*20, '-'*20, '-'*20, '-'*20, '-'*20, '-'*20, '-'*20, '-'*8))
  651.  
  652.             # TODO: Sort the list by (nick)name
  653.             for abe in all_entries:
  654.                 print formatter.compose((abe.name, abe.title, abe.firstname, abe.lastname, abe.fax, abe.notes, abe.groups, str(abe.recno)))
  655.         else:
  656.             print "(None)"
  657.  
  658.         print
  659.  
  660.  
  661.  
  662.     def do_show(self, args):
  663.         """
  664.         Show an entry (all details).
  665.         show [entry]
  666.         details [entry]
  667.         """
  668.         name = self.get_nickname(args, fail_if_match=False)
  669.         if not name: return
  670.  
  671.         rec = self.db.select(['name'], [name])
  672.         if rec:
  673.             abe = fax.AddressBookEntry(rec[0])
  674.  
  675.             formatter = utils.TextFormatter(
  676.                             (
  677.                                 {'width': 28, 'margin' : 2},
  678.                                 {'width': 58, 'margin' : 2},
  679.                             )
  680.                         )
  681.  
  682.             print utils.bold("\n%s\n" % name)
  683.  
  684.             print formatter.compose(("Name:", abe.name))
  685.             print formatter.compose(("Title:", abe.title))
  686.             print formatter.compose(("First Name:", abe.firstname))
  687.             print formatter.compose(("Last Name:", abe.lastname))
  688.             print formatter.compose(("Fax Number:", abe.fax))
  689.             print formatter.compose(("Member of Group(s):", abe.groups))
  690.             print formatter.compose(("Notes:", abe.notes))
  691.             print formatter.compose(("(recno):", str(abe.recno)))
  692.  
  693.         else:
  694.             print utils.red("error: Entry name not found. Use 'list entries' to view all entry names.")
  695.  
  696.         print
  697.  
  698.     do_details = do_show
  699.  
  700.     def do_rm(self, args):
  701.         """
  702.         Remove an entry.
  703.         rm [entry]
  704.         del [entry]
  705.         """
  706.         nickname = self.get_nickname(args, fail_if_match=False)
  707.         if not nickname: return
  708.  
  709.         self.db.delete(['name'], [nickname])
  710.  
  711.         print
  712.  
  713.     do_del = do_rm
  714.  
  715.     def do_rmgrp(self, args):
  716.         """
  717.         Remove a group.
  718.         rmgrp [group]
  719.         delgrp [group]
  720.         """
  721.         group = self.get_groupname(args, fail_if_match=False)
  722.         if not group: return
  723.  
  724.         self.db.DeleteGroup(group)
  725.  
  726.         print
  727.  
  728.     do_delgrp = do_rmgrp
  729.  
  730.  
  731.     def do_about(self, args):
  732.         """About fab."""
  733.         utils.log_title(__title__, __version__)
  734.         additional_copyright()
  735.  
  736.  
  737.  
  738.  
  739. mode = GUI_MODE
  740. mode_specified = False
  741.  
  742. try:
  743.     opts, args = getopt.getopt(sys.argv[1:], 'l:hgiu', 
  744.         ['level=', 'help', 'help-rest', 'help-man',
  745.          'help-desc', 'gui', 'interactive'])
  746.  
  747. except getopt.GetoptError:
  748.     usage()
  749.  
  750. if os.getenv("HPLIP_DEBUG"):
  751.     log.set_level('debug')
  752.  
  753. for o, a in opts:
  754.     if o in ('-l', '--logging'):
  755.         log_level = a.lower().strip()
  756.         if not log.set_level(log_level):
  757.             usage()
  758.  
  759.     elif o == '-g':
  760.         log.set_level('debug')
  761.  
  762.     elif o in ('-h', '--help'):
  763.         usage()
  764.  
  765.     elif o == '--help-rest':
  766.         usage('rest')
  767.  
  768.     elif o == '--help-man':
  769.         usage('man')
  770.  
  771.     elif o == '--help-desc':
  772.         print __doc__,
  773.         sys.exit(0)
  774.  
  775.     elif o in ('-i', '--interactive'):
  776.         if mode_specified:
  777.             log.error("You may only specify a single mode as a parameter (-i or -u).")
  778.             sys.exit(1)
  779.  
  780.         mode = INTERACTIVE_MODE
  781.         mode_specified = True
  782.  
  783.     elif o in ('-u', '--gui'):
  784.         if mode_specified:
  785.             log.error("You may only specify a single mode as a parameter (-i or -u).")
  786.             sys.exit(1)
  787.  
  788.         mode = GUI_MODE
  789.         mode_specified = True
  790.  
  791. utils.log_title(__title__, __version__)
  792. additional_copyright()
  793.  
  794. # Security: Do *not* create files that other users can muck around with
  795. os.umask (0077)
  796.  
  797. if mode == GUI_MODE:
  798.     if not os.getenv('DISPLAY'):
  799.         mode = NON_INTERACTIVE_MODE
  800.     elif not utils.checkPyQtImport():
  801.         mode = NON_INTERACTIVE_MODE
  802.  
  803. if mode == GUI_MODE:
  804.     from qt import *
  805.     from ui.faxaddrbookform import FaxAddrBookForm
  806.  
  807.     app = None
  808.     addrbook = None
  809.  
  810.     # create the main application object
  811.     app = QApplication(sys.argv)
  812.  
  813.     addrbook = FaxAddrBookForm()
  814.     addrbook.show()
  815.     app.setMainWidget(addrbook)
  816.  
  817.     user_config = os.path.expanduser('~/.hplip.conf')
  818.     loc = utils.loadTranslators(app, user_config)
  819.  
  820.     try:
  821.         log.debug("Starting GUI loop...")
  822.         app.exec_loop()
  823.     except KeyboardInterrupt:
  824.         pass
  825.     except:
  826.         log.exception()
  827.  
  828.     sys.exit(0)
  829.  
  830. else: # INTERACTIVE_MODE
  831.     try:
  832.         from fax import fax
  833.     except ImportError:
  834.         # This can fail on Python < 2.3 due to the datetime module
  835.         log.error("Fax address book disabled - Python 2.3+ required.")
  836.         sys.exit(1)    
  837.  
  838.     console = Console()
  839.     try:
  840.         try:
  841.             console.cmdloop()
  842.         except KeyboardInterrupt:
  843.             log.error("Aborted.")
  844.         except Exception, e:
  845.             #log.error("An error occured: %s" % e)
  846.             log.exception()
  847.     finally:
  848.         pass
  849.